﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevelopTool.Bll.CodeBlls;
using DevelopTool.Bll.SchedulerBlls;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Model.CodeModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.SchedulerModels;
using Sunny.UI;

namespace DevelopTool.Forms.SchedulerForms
{
    public partial class SchedulerForm_Code : BaseUIPage
    {
        /// <summary>
        /// 定时任务对象
        /// </summary>
        private Scheduler scheduler;

        /// <summary>
        /// 定时任务sql对象
        /// </summary>
        private Scheduler_Code scheduler_Code;


        private Scheduler_CodeBll scheduler_CodeBll;

        private CodeBll codeBll;

        public SchedulerForm_Code(Scheduler scheduler)
        {
            InitializeComponent();

            this.scheduler = scheduler;

        }

        private void SchedulerForm_Code_Load(object sender, EventArgs e)
        {
            this.scheduler_CodeBll = new Scheduler_CodeBll();
            this.codeBll = new CodeBll();

            this.scheduler_Code = scheduler_CodeBll.GetBySchedulerId(scheduler.Id);

            var codeList = this.codeBll.GetList();

            var treeView = this.cboCode.TreeView;

            TreeNode root = new TreeNode("代码列表")
            {
                Tag = new Code() { Id = 0, NodeType = MenuNodeType.Folder }
            };

            BindCboTreeViewData(root, codeList);
            treeView.Nodes.Add(root);
            root.Expand();

           

        }

        /// <summary>
        /// 递归绑定代码选择下拉框数据
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="list"></param>
        private void BindCboTreeViewData(TreeNode parentNode, List<Code> list)
        {
            var parentMenu = parentNode.Tag as Code;

            foreach (Code code in list.Where(it => it.NodePid == parentMenu.Id))
            {
                TreeNode node = new TreeNode(code.NodeName)
                {
                    Tag = code
                };

                parentNode.Nodes.Add(node);

                if (this.scheduler_Code != null && this.scheduler_Code.Code != null)
                {
                    if (code.NodeName == this.scheduler_Code.Code.NodeName)
                        this.cboCode.SelectedNode = node;//选中
                }

                BindCboTreeViewData(node, list);

            }
        }


        /// <summary>
        /// 保存，在父窗口中调用
        /// </summary>
        public override bool Call()
        {
            if (!(this.cboCode.SelectedNode.Tag is Code code) || code.NodeType != MenuNodeType.Node)
            {
                this.ShowWarningNotifier("请选择需要执行的代码");
                return false;
            }

            if (this.scheduler_Code == null)
            {
                this.scheduler_Code = new Scheduler_Code() { SchedulerId = this.scheduler.Id };
            }
            this.scheduler_Code.Code = code;

            this.scheduler_Code = this.scheduler_CodeBll.Save(this.scheduler_Code);

            return true;
        }
    }
}
